<?php

namespace frontend\models;

use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use yii\web\IdentityInterface;
use common\models\grade\Grade;
use yii\helpers\ArrayHelper;
/**
 * This is the model class for table "member".
 *
 * @property integer $id
 * @property string $username
 * @property string $auth_key
 * @property string $password_hash
 * @property string $password_reset_token
 * @property string $phone
 * @property string $idcard
 * @property string $ip
 * @property string $faceimg
 * @property string $sex
 * @property string $birthday
 * @property string $age
 * @property string $comefrom
 * @property string $work
 * @property string $address
 * @property string $realname
 * @property string $email
 * @property string $grade
 * @property integer $status
 * @property integer $created_at
 * @property integer $updated_at
 */
class Member extends ActiveRecord implements IdentityInterface
{
public $verifyCode;
    public $password;
    public $repassword;
    private $_statusLabel;
    private $_gradeLabel;
    private $_isopenLabel;
    private $_qiyongLabel;
    const STATUS_DELETED = 0; //再审核
    const STATUS_ACTIVE = 10; //待审核
    const STATUS_SUCCESS = 20; //已审核
    const STATUS_WEI = 30; //还未申请
    const QIYONG_SUCCESS = 1; //已启用
    const QIYONG_DELETED = 2; //停用

    const IS_PAID = 1;
    const NO_PAID = 0;

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
            ],
        ];
    }
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'member';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
             ['verifyCode', 'captcha', 'message'=>'验证码错误','captchaAction'=>'/auth/captcha'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'username' => Yii::t('app', 'Username'),
            'auth_key' => Yii::t('app', 'Auth Key'),
            'password_hash' => Yii::t('app', 'Password Hash'),
            'password_reset_token' => Yii::t('app', 'Password Reset Token'),
            'xingming' => Yii::t('app', '姓名'),
            'phone' => Yii::t('app', 'Phone'),
            'email' => Yii::t('app', 'Email'),
            'work' => Yii::t('app', '职务'),
            'company' => Yii::t('app', '企业名称'),
            'loc_province' => Yii::t('app', '企业所在省份'),
            'loc_city' => Yii::t('app', '企业所在市'),
            'loc_town' => Yii::t('app', '企业所在县'),
            'address' => Yii::t('app', '企业详细地址'),
            'ip' => Yii::t('app', 'Ip'),
            'farentel' => Yii::t('app', '企业电话'),
            'grnum' => Yii::t('app', '员工总数'),
            'zhucezj' => Yii::t('app', '注册资金'),
            'faren' => Yii::t('app', '联系人姓名'),
            'farenphone' => Yii::t('app', '联系人电话'),
            'farenemail' => Yii::t('app', '联系人电子邮箱'),
            'yyzhizao' => Yii::t('app', '统一社会信用代码'),
            'qychengli' => Yii::t('app', '营业执照有效期'),
            'qychenglizhi' => Yii::t('app', '营业执照有效期'),
            'zcdizhi' => Yii::t('app', '营业执照所在地'),
            'jyfanwei' => Yii::t('app', '法定经营范围'),
            'yyzhizhaoimg' => Yii::t('app', '营业执照电子版'),
            'card' => Yii::t('app', '法人身份证号码'),
            'cardz' => Yii::t('app', '身份证正面电子版'),
            'cardf' => Yii::t('app', '身份证背面电子版'),
            'fwdengjiimg' => Yii::t('app', '相关资质（如餐饮许可证）'),
            //'grade' => Yii::t('app', '会员等级'),
            'status' => Yii::t('app', 'Status'),
            'reason' => Yii::t('app', '驳回原因'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
            'isopen' => Yii::t('app', '是否支付'),
            'qiyong' => Yii::t('app', '启用/否'),
            'description' => Yii::t('app', '会员描述'),
            'content' => Yii::t('app', '会员简介'),
            
        ];
    }
    public static function getArrayGrade(){
        return ArrayHelper::map(Grade::find()->andWhere(['status'=>Grade::STATUS_ACTIVE])->all(), 'id', 'name');
    }
    public function getGradeLabel()
    {

        if ($this->_gradeLabel === null) {
            $grade = self::getArrayGrade();
            $this->_gradeLabel =  array_key_exists($this->grade,self::getArrayGrade()) ? $grade[$this->grade] : '无';
        }
        return $this->_gradeLabel;
    }    
    /**
     * @inheritdoc
     */
    public function getStatusLabel()
    {
        if ($this->_statusLabel === null) {
            $statuses = self::getArrayStatus();
            $this->_statusLabel = array_key_exists($this->status,self::getArrayStatus()) ? $statuses[$this->status] : '无';
        }
        return $this->_statusLabel;
    }
    /**
     * @inheritdoc
     */
    public function getQiyongLabel()
    {
        if ($this->_qiyongLabel === null) {
            $statuses = self::getArrayQiyong();
            $this->_qiyongLabel = array_key_exists($this->qiyong,self::getArrayQiyong()) ? $statuses[$this->qiyong] : '无';
        }
        return $this->_qiyongLabel;
    }
    /**
     * @inheritdoc
     */
    public static function getArrayQiyong()
    {
        return [
            self::QIYONG_SUCCESS => Yii::t('app', '启用'),
            self::QIYONG_DELETED => Yii::t('app', '停用'),

        ];
    }
    public function getQiyong($status)
    {
        return ($status==1)?"已启用":"停用";
    }
    /**
     * @inheritdoc
     */
    public static function getArrayStatus()
    {
        return [
            self::STATUS_SUCCESS => Yii::t('app', '已审核'),
            self::STATUS_ACTIVE => Yii::t('app', '待审核'),
            self::STATUS_DELETED => Yii::t('app', '再审核'),
            self::STATUS_WEI => Yii::t('app', '新注册'),
        ];
    }
    public function getIsopenLabel()
    {
        if ($this->_isopenLabel === null) {
            $isopens = self::getArrayIsopen();
            $this->_isopenLabel = array_key_exists($this->isopen,self::getArrayIsopen()) ? $isopens[$this->isopen] : '无';
        }
        return $this->_isopenLabel;
    }
    /**
     * @inheritdoc
     */
    public static function getArrayIsopen()
    {
        return [
            self::IS_PAID => Yii::t('app', '已支付'),
            self::NO_PAID => Yii::t('app', '未支付'),
        ];
    }
    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return static::findOne(['id' => $id]);
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
    }

    /**
     * Finds user by username
     *
     * @param string $username
     * @return static|null
     */
    public static function findByUsername($username)
    {
        return static::findOne(['username' => $username]);
    }
    public static function findByPhone($username)
    {
        return static::findOne(['phone' => $username]);
    }
    public static function findByEmail($username)
    {
        return static::findOne(['Email' => $username]);
    }

    /**
     * Finds user by password reset token
     *
     * @param string $token password reset token
     * @return static|null
     */
    public static function findByPasswordResetToken($token)
    {
        if (!static::isPasswordResetTokenValid($token)) {
            return null;
        }

        return static::findOne([
            'password_reset_token' => $token
        ]);
    }
    
    /**
     * Finds out if password reset token is valid
     *
     * @param string $token password reset token
     * @return boolean
     */
    public static function isPasswordResetTokenValid($token)
    {
        if (empty($token)) {
            return false;
        }
        $expire = Yii::$app->params['user.passwordResetTokenExpire'];
        $parts = explode('_', $token);
        $timestamp = (int) end($parts);
        return $timestamp + $expire >= time();
    }

    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->getPrimaryKey();
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey()
    {
        return $this->auth_key;
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey)
    {
        return $this->getAuthKey() === $authKey;
    }

    /**
     * Validates password
     *
     * @param string $password password to validate
     * @return boolean if password provided is valid for current user
     */
    public function validatePassword($password)
    {
        return Yii::$app->security->validatePassword($password, $this->password_hash);
    }
    public function validatePhone($phone)
    {
        return 0;
    }


    /**
     * Generates password hash from password and sets it to the model
     *
     * @param string $password
     */
    public function setPassword($password)
    {
        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
    }

    /**
     * Generates "remember me" authentication key
     */
    public function generateAuthKey()
    {
        $this->auth_key = Yii::$app->security->generateRandomString();
    }

    /**
     * Generates new password reset token
     */
    public function generatePasswordResetToken()
    {
        $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
    }
    /**
     * Removes password reset token
     */
    public function removePasswordResetToken()
    {
        $this->password_reset_token = null;
    }
}
